#!/usr/bin/env python
from waveapi import events
from waveapi import model
from waveapi import robot
import serbian

def OnRobotAdded(properties, context):
  context.GetRootWavelet().CreateBlip().GetDocument().SetText("Hi. My name is CirBot and I can help you with Serian cyrillic. I'll convert every blip containing Serbian cyrillic text to its latin representation.") 

def OnBlipSubmitted(properties, context):
  blip=context.GetBlipById(properties['blipId'])
  if(blip.GetCreator().find('cirbot@appspot.com')==-1):
    doc = blip.GetDocument()
    text=doc.GetText()
    if(text.__len__()>0):
      j=None
      for i in blip.GetChildBlipIds():
        i=context.GetBlipById(i)
        if(i.GetCreator().find('cirbot@appspot.com')>-1):
          j=i;
      if(j==None):
        j=blip.CreateChild()
      j.GetDocument().SetText(serbian.srSplitDigraphs(serbian.srCyrillicToLatin(text)))
    
if __name__ == '__main__':
  myRobot = robot.Robot('CirBot',image_url='http://cirbot.appspot.com/cirbot.png',version='1',profile_url='http://cirbot.appspot.com/')
  myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
  myRobot.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmitted)
  myRobot.Run()
